import axios from 'axios'
import qs from 'qs';
import router from '../router'
import store from '../store/index.js'
import {
    ElMessageBox,
    ElMessage,
    ElLoading
} from 'element-plus'
import {
    getToken, removeToken, removeUserInfo
} from '@/utils/auth'

let urlArr = [
    '/LoginAndReg/UserEdit',
    '/UserInfo/UserEdit',
    '/UserInfo/UserToAuthor',
    '/ScriptWriting/ScriptWritingEdit',
    '/TradeInfo/TradeInfoAdd',
    '/WorksCallup/WorksCallupEdit',
    '/WorksCallup/SubmitWriting',
    '/LoginAndReg/ResetPassword',
    '/WorksCallup/SubmitWriting',
]



// axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
// 创建axios实例
let service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: process.env.VUE_APP_BASE_API,
    // 超时
    // timeout: 60000*3,
    headers: {'content-type': 'application/x-www-form-urlencoded'},
    withCredentials: true,
})

// request拦截器
service.interceptors.request.use(
    config => {
        // if (getToken()) {
        //   config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        // }
        // config.headers['userId'] = '38'



        if (urlArr.indexOf(config.url) > -1) {
            config.headers['content-type'] = 'application/json'
        } else {
            if (!(config.url.indexOf('UpLoadImg') > -1)) {
                config.data = qs.stringify(config.data)
            }
        }
        return config
    },
    error => {
        console.log(error)
        Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(res => {
        let code = res.data.code
        // if (code === -1) {
        //     ElMessageBox.confirm(
        //         '登录状态已过期，您可以继续留在该页面，或者重新登录',
        //         '系统提示', {
        //             confirmButtonText: '重新登录',
        //             cancelButtonText: '取消',
        //             type: 'warning'
        //         }
        //     ).then(() => {
        //         removeToken()
        //         removeUserInfo()
        //         store.commit("setUser", {});
        //         router.push({path: '/'})
        //     })
        // } else {
        //     return res.data
        // }


        return res.data
    },
    error => {



        console.log('呵呵',error)
        let status = error.response.data.code
        let msg = error.response.data.message
        if (status == 401) {
            ElMessageBox.confirm(
                '登录状态已过期，请重新登录',
                '系统提示', {
                    confirmButtonText: '重新登录',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).then(() => {
                removeToken()
                window.location.reload()
            })
        } else if (status >= 400) {
            ElMessage({
                message: msg,
                dangerouslyUseHTMLString: true,
                type: 'error',
                duration: 5 * 1000
            })
        } else {
            console.log('err' + error)
            ElMessage({
                message: error.message,
                type: 'error',
                duration: 5 * 1000
            })
        }
        return Promise.reject(error)
    }
)

export default service
